Skip to content

Implement release threshold configuration for DeviceMemoryResource performance optimization - #875

Merged
leofang merged 9 commits into
mainfrom
copilot/fix-771
Aug 21, 2025
Merged

Implement release threshold configuration for DeviceMemoryResource performance optimization#875
leofang merged 9 commits into
mainfrom
copilot/fix-771

Conversation

Copilot AI commented Aug 21, 2025

Copy link
Copy Markdown
Contributor

This PR implements a performance optimization for DeviceMemoryResource by configuring the memory pool release threshold, addressing slow allocation performance when there are no active allocations.

Problem

As reported in issue #756, allocation via DeviceMemoryResource can be very slow when there are no active allocations. This occurs because the default memory pool is configured with a release threshold of 0, meaning memory is immediately released back to the OS when there are no active suballocations. Subsequent allocations then require expensive OS memory requests.

Solution

This PR upstreams the workaround from CCCL to cuda-core by modifying DeviceMemoryResource.__init__() to:

  1. Check the current release threshold using cuMemPoolGetAttribute
  2. If the threshold is 0 (default), set it to maximum (0xFFFFFFFFFFFFFFFF) using cuMemPoolSetAttribute
  3. This retains memory in the pool for better allocation performance in steady-state scenarios

Implementation Details

  • Core change: 22 lines added to DeviceMemoryResource.__init__() in cuda_core/cuda/core/experimental/_memory.py
  • Error handling: Wrapped in try-except for backward compatibility with older CUDA drivers
  • Documentation: Added comprehensive comments explaining the optimization
  • Testing: Added test case to verify proper initialization
  • Example: Created performance demonstration script

The implementation follows the exact same pattern as the CCCL workaround, using identical CUDA driver APIs and constants.

Example Usage

from cuda.core.experimental import Device, DeviceMemoryResource

device = Device()
mr = DeviceMemoryResource(device.device_id)  # Now automatically optimized

# Subsequent allocations will be faster due to memory retention
buffer = mr.allocate(1024)
buffer.close()

The optimization is transparent to users and maintains full backward compatibility.

Fixes #771.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@copy-pr-bot

copy-pr-bot Bot commented Aug 21, 2025

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Copilot AI and others added 3 commits August 21, 2025 02:30
…rformance optimization

Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>
…ization

Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>
Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>
Copilot AI changed the title [WIP] Use an increased release threshold for the default async pool when using DeviceMemoryResource Implement release threshold configuration for DeviceMemoryResource performance optimization Aug 21, 2025
Copilot AI requested a review from leofang August 21, 2025 02:36

@leofang leofang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot

Comment thread cuda_core/cuda/core/experimental/_memory.py Outdated
Comment thread cuda_core/examples/memory_performance_demo.py Outdated
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in CCCL Aug 21, 2025
Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>
Copilot AI requested a review from leofang August 21, 2025 02:50
@leofang

leofang commented Aug 21, 2025

Copy link
Copy Markdown
Member

@copilot please add a release note entry noting the performance optimization in device memory management. No need to be too explicit about what's the optimization we did in this PR.

Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>

Copilot AI commented Aug 21, 2025

Copy link
Copy Markdown
Contributor Author

@copilot please add a release note entry noting the performance optimization in device memory management. No need to be too explicit about what's the optimization we did in this PR.

Added release note entry for the DeviceMemoryResource performance optimization to the 0.X.Y release notes. (6a2c4f4)

@leofang

leofang commented Aug 21, 2025

Copy link
Copy Markdown
Member

@copilot please apply the labels from the issue we're fixing to this PR, thanks

Copilot AI commented Aug 21, 2025

Copy link
Copy Markdown
Contributor Author

@copilot please apply the labels from the issue we're fixing to this PR, thanks

I can see that issue #771 has the following labels: enhancement, P0, and cuda.core. However, I don't have access to GitHub API functions that allow me to modify PR properties like adding labels. Could you please apply these labels to the PR, or let me know if there's another way I can help with this?

@leofang leofang added enhancement Any code-related improvements P0 High priority - Must do! labels Aug 21, 2025
@leofang leofang added the cuda.core Everything related to the cuda.core module label Aug 21, 2025
@leofang leofang added this to the cuda.core beta 7 milestone Aug 21, 2025
@leofang
leofang requested a review from shwina August 21, 2025 03:10
@leofang
leofang marked this pull request as ready for review August 21, 2025 03:10
@leofang

leofang commented Aug 21, 2025

Copy link
Copy Markdown
Member

/ok to test 6a2c4f4

@github-actions

This comment has been minimized.

Comment thread cuda_core/tests/test_memory.py
Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>
Copilot AI requested a review from leofang August 21, 2025 03:48
Comment thread cuda_core/cuda/core/experimental/_memory.py
Comment thread cuda_core/docs/source/release/0.X.Y-notes.rst Outdated
…date release note

Co-authored-by: kkraus14 <3665167+kkraus14@users.noreply.github.com>
Copilot AI requested a review from kkraus14 August 21, 2025 04:30
@kkraus14

Copy link
Copy Markdown
Collaborator

/ok to test 2a8b536

kkraus14
kkraus14 previously approved these changes Aug 21, 2025
Comment on lines +269 to +270
if not device.properties.memory_pools_supported:
pytest.skip("memory pools not supported")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a (happy) coincidence or the copilot actually understands that this is legit code (accessing device properties without calling .set_current() is OK)? Too good to be true!!!

Comment thread cuda_core/cuda/core/experimental/_memory.py Outdated
Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>

@leofang leofang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Keith already approved, the CI was green, and the last commit was doc-only, let me admin-merge this. Thanks Keith & @copilot!

@leofang
leofang merged commit 85da64b into main Aug 21, 2025
1 check passed
@leofang
leofang deleted the copilot/fix-771 branch August 21, 2025 13:16
@github-project-automation github-project-automation Bot moved this from In Progress to Done in CCCL Aug 21, 2025
@github-actions

Copy link
Copy Markdown
Doc Preview CI
Preview removed because the pull request was closed or merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module enhancement Any code-related improvements P0 High priority - Must do!

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Use an increased release threshold for the default async pool when using DeviceMemoryResource

3 participants